package com.lagou.edu.controller;

import com.lagou.edu.dao.ResumeDao;
import com.lagou.edu.pojo.Resume;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Optional;

@Controller
@RequestMapping("/resume")
public class ResumeController {

    /**
     * spring容器和springmvc容器是有层次的（父子容器），
     * spring容器：service对象和dao对象，
     * springmvc容器：controller对象，可以引用到spring容器中的对象
     */

    @Autowired
    private ResumeDao resumeDao;

    @RequestMapping("/queryAll")
    @ResponseBody
    public List<Resume> queryAll(){
        return resumeDao.queryResumeList();
    }

    @RequestMapping("/queryA")
    @ResponseBody
    public List<Resume> queryA(){
        return resumeDao.queryResumeList();
    }

    @RequestMapping("/queryList")
    @ResponseBody
    public ModelAndView queryList(){
        ModelAndView modelAndView = new ModelAndView();
        List<Resume> resumeList = resumeDao.queryResumeList();;
        modelAndView.addObject("list",resumeList);
        modelAndView.setViewName("list");
        return modelAndView;
    }


    @RequestMapping("/logon")
    @ResponseBody
    public ModelAndView findAccounts(HttpServletRequest request,String username, String password){

        ModelAndView modelAndView = new ModelAndView();
        HttpSession session = request.getSession();
        session.setAttribute("username",username);
        if(username!=null && username.equals("admin")&&password.equals("admin")){//用户、密码验证
            List<Resume> resumeList =  resumeDao.queryResumeList();;
            modelAndView.addObject("list",resumeList);
            modelAndView.setViewName("list");
        }else{
            modelAndView.addObject("msg","用户名或密码不正确！");
            modelAndView.setViewName("error");
        }
        return modelAndView;
    }

    @RequestMapping("/add")
    public String addAccount(){
        return "add";
    }

    @RequestMapping("/detail")
    public ModelAndView detailAccount(HttpServletRequest request){
        Long id = Long.parseLong(request.getQueryString()) ;
        //Optional<Resume> optional = resumeDao.findById(id);
       // Account account = resumeDao.accountService.queryOne(cardno);
        Resume resume = resumeDao.queryResumeOne(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("resume",resume);
        modelAndView.setViewName("detail");
        return modelAndView;
    }

    @RequestMapping("/save")
    public String saveAccount(Long id,String name,String address,String phone){
        Resume resume = new Resume();
        resume.setId(id);
        resume.setName(name);
        resume.setAddress(address);
        resume.setPhone(phone);
        resumeDao.save(resume);

        return "redirect:/resume/queryList";
    }

    @RequestMapping("/update")
    public ModelAndView updateAccount(HttpServletRequest request){
        Long id = Long.parseLong(request.getQueryString()) ;
        Resume resume = resumeDao.queryResumeOne(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("resume",resume);
        modelAndView.setViewName("update");
        return modelAndView;
    }

    @RequestMapping("/updateSave")
    public String updateSaveAccount(Long id,String name,String address,String phone){
        Resume resume = new Resume();
        resume.setId(id);
        resume.setName(name);
        resume.setAddress(address);
        resume.setPhone(phone);
        resumeDao.save(resume);
        return "redirect:/resume/queryList";

    }

    @RequestMapping(value = "/delete" )
    public String deletAccount(HttpServletRequest request){
        Long id = Long.parseLong(request.getQueryString()) ;
        resumeDao.deleteById(id);
        return "redirect:/resume/queryList";
    }

}
